package com.sl.au.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.sl.au.entity.MyMonetTotalView;
import com.sl.au.entity.User;
import com.sl.au.service.MyMoneyTotalViewService;
import com.sl.au.service.QueryService;
@Service("MyMoneyTotalViewService")
@Transactional
public class MyMoneyTotalViewServiceImpl implements MyMoneyTotalViewService {

	@Autowired
	QueryService queryService;
	
	@Override
	public List<MyMonetTotalView> getMyMoneyTotal(int start, int length, Map<String, Object> condition, User user,
			String orderName, String dir) {
		// TODO Auto-generated method stub
		@SuppressWarnings("unchecked")
		HashMap<String, Object>numMap=(HashMap<String, Object>)condition.get("numMap");
		Map<String,Object>namedParamsMap=new HashMap<String,Object>();
		StringBuilder hql=queryService.buildCombinedQueryHql("MyMonetTotalView",null, condition, namedParamsMap,null, user);
		for(String key:numMap.keySet()){
			Object value=numMap.get(key);
			if(key == "memo"){
				hql.append("and (e.memo like '%"+value.toString()+"%') or (e.useContent like '%"+value.toString()+"%')");
			} 
			else{
				if (value instanceof String){
					 if(StringUtils.hasText((String)value)){
						 System.out.println(key+" + "+value.toString());
						 hql.append("and (e."+key+" in ("+value.toString()+"))");
					 }
				}
			}
		}
		String dataFilter=condition.get("dataFilter").toString();
		if(StringUtils.hasText(dataFilter)){
			//this.contractDataFilter(hql, dataFilter, user);
		}else{
			//return null;
		}
		hql.append(" order by " + orderName + " " + dir);
		System.out.println(hql.toString());
		Query query=queryService.createQueryHql(hql.toString());
		query.setFirstResult(start).setMaxResults(length);
		List<MyMonetTotalView> aa =  queryService.executeHql(query, namedParamsMap);
		return queryService.executeHql(query, namedParamsMap);
	}

}
